package com.ruoyi.zhgd.controller;

import java.util.*;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.zhgd.domain.vo.TensionRecordingVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zhgd.domain.vo.ZjNLaboratoryTensionRequiresVo;
import com.ruoyi.zhgd.domain.bo.ZjNLaboratoryTensionRequiresBo;
import com.ruoyi.zhgd.service.IZjNLaboratoryTensionRequiresService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 张拉技术要求参数
 *
 * @author tll
 * @date 2023-11-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhgd/nLaboratoryTensionRequires")
public class ZjNLaboratoryTensionRequiresController extends BaseController {

    private final IZjNLaboratoryTensionRequiresService iZjNLaboratoryTensionRequiresService;

    /**
     * 查询张拉技术要求参数列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<ZjNLaboratoryTensionRequiresVo> list(ZjNLaboratoryTensionRequiresBo bo, PageQuery pageQuery) {
        return iZjNLaboratoryTensionRequiresService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出张拉技术要求参数列表
     */
    @SaCheckLogin
    @Log(title = "张拉技术要求参数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ZjNLaboratoryTensionRequiresBo bo, HttpServletResponse response) {
        List<ZjNLaboratoryTensionRequiresVo> list = iZjNLaboratoryTensionRequiresService.queryList(bo);
        ExcelUtil.exportExcel(list, "张拉技术要求参数", ZjNLaboratoryTensionRequiresVo.class, response);
    }

    /**
     * 获取张拉技术要求参数详细信息
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/{id}")
    public R<ZjNLaboratoryTensionRequiresVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iZjNLaboratoryTensionRequiresService.queryById(id));
    }

    /**
     * 新增张拉技术要求参数
     */
    @SaCheckLogin
    @Log(title = "张拉技术要求参数", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ZjNLaboratoryTensionRequiresBo bo) {
        return toAjax(iZjNLaboratoryTensionRequiresService.insertByBo(bo));
    }

    /**
     * 修改张拉技术要求参数
     */
    @SaCheckLogin
    @Log(title = "张拉技术要求参数", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ZjNLaboratoryTensionRequiresBo bo) {
        return toAjax(iZjNLaboratoryTensionRequiresService.updateByBo(bo));
    }

    /**
     * 删除张拉技术要求参数
     *
     * @param ids 主键串
     */
    @SaCheckLogin
    @Log(title = "张拉技术要求参数", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iZjNLaboratoryTensionRequiresService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @SaIgnore
    @GetMapping("/isQualified")
    public R isQualified(@NotNull @RequestParam("linkCode") String linkCode) {
        // 等待张拉结果表数据采集完成后进行判定 等待时间 10分钟
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                iZjNLaboratoryTensionRequiresService.isQualified(linkCode);
            }
        };
        timer.schedule(timerTask, 10 * 60 * 1000);
        return R.ok("", linkCode);
    }

    /**
     * 全部张拉手动校验是否合格并更新数据
     * @return
     */
    @SaCheckLogin
    @PostMapping("/isQualifiedList")
    public R isQualifiedList(TensionRecordingVo vo) {
        iZjNLaboratoryTensionRequiresService.isQualifiedList(vo);
        return R.ok();
    }

    /**
     * 手动校验
     * @return
     */
    @SaCheckLogin
    @PostMapping("/handCheck")
    public R handCheck(@Validated(EditGroup.class) @RequestBody ZjNLaboratoryTensionRequiresBo bo) {
        iZjNLaboratoryTensionRequiresService.handCheck(bo);
        return R.ok();
    }

}
